package array;

/**
 * 自定义数组
 */
public class Array<E> {
    private E[] data;
    private int size;

    /**
     *
     * @param capacity 初始数组容量大小
     */
    public  Array(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array(){
        this(10);
    }

    public Array(E[] arr){
        data = (E[]) new Object[arr.length];
        for(int i=0;i<arr.length;i++){
            data[i] = arr[i];
        }
        size = arr.length;
    }

    public int getSize(){
        return size;
    }

    public int getCapacity(){
        return data.length;
    }

    //数组是否为空
    public boolean isEmpty(){
        return size == 0;
    }

    //向所有元素最后面添加元素  时间复杂度o(1)
    public void addLast(E e){
        add(size,e);
    }

    //向所有元素最前面添加元素 o（n）
    public void addFirst(E e){
        add(0,e);
    }

    //在第index位置插入一个新元素e o(n/2) = o(n)
    public void add(int index,E e){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("Add faild,Array is null,requried index >= 0 && index <=size ");
        }
        if(size == data.length){
            resize(2*data.length);
        }
        for(int i= size-1;i>=index;i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }

    //获取index索引的值
    public E get(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("Get failed,Index is  illegal");
        }
        return data[index];
    }

    public E getLast(){
        return get(size - 1);
    }
    public E getFirst(){
        return get(0);
    }

    //修改index位置索引的值
    public void set(int index,E e){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("Set failed,Index is  illegal");
        }
        data[index]  = e;
    }

    //查找数组中是否包含e
    public boolean contains(E e){
        for (int i = 0; i < size; i++) {
            if(data[i] .equals(e) ){
                return true;
            }
        }
        return false;
    }

    //查找数组中元素e所在索引，如果不存在，返回-1；
    public int find(E e){
        for (int i = 0; i < size; i++) {
            if(data[i] .equals(e)){
                return i;
            }
        }
        return -1;
    }

    // 从数组中删除index位置的元素，并且返回删除的元素
    public E remove(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("Removed faild");
        }
        E ret = data[index];
        for(int i= index+1;i<size;i++){
            data[i-1] = data[i];
        }
        size --;
        data[size] = null; //loitering Objects != memory leak
        //解决复杂度震荡
        if(size == data.length/4 && data.length /2 != 0){
            resize(data.length / 2);
        }
        return ret;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    //从数组中删除元素e
    public void removeElement(E e){
        int index = find(e);
        if(index != -1){
            remove(index);
        }
    }

    public void swap(int i,int j){
        if(i < 0 || i >= size || j < 0 || j >= size){
            throw new IllegalArgumentException("illegal");
        }

        E t = data[i];
        data[i] = data[j];
        data[j] = t;
    }

    @Override
    public String toString() {
        StringBuffer res = new StringBuffer();
        res.append(String.format("Array:size= %d,capacity=%d\n",size,data.length));
        res.append("[");
        for(int i=0;i<size;i++){
            res.append(data[i]);
            if(i != size -1){
                res.append(",");
            }
        }
        res.append("]");
        return res.toString();
    }

    private void resize(int newCapacity){
        E[] newData = (E[]) new Object[newCapacity];
        for(int i = 0; i< size;i++){
            newData[i]  = data[i];
        }
        data = newData;

    }
}
